#include "QtGobang.h"
#include <QPainter>
#include <QMouseEvent>
#include <QMessageBox>

QtGobang::QtGobang(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    for (int i = 0; i < Row; i++)
        for (int j = 0; j < Row; j++)
            CheckBoard[i][j] = 0;
    ChessRow = -1;
    ChessCol = -1;
    Player = true;
    GameOver = false;
    update();
}

void QtGobang::paintEvent(QPaintEvent*)
{
    QPainter pt(this);
    pt.setRenderHint(QPainter::Antialiasing, true);
    for (int i = 0; i < Row; i++)
    {
        pt.drawLine(20 + 40 * i, 20, 20 + 40 * i, 580);
        pt.drawLine(20, 20 + 40 * i, 580, 20 + 40 * i);
    }
    QBrush br;
    br.setStyle(Qt::SolidPattern);
    if (CheckBoard[ChessRow][ChessCol] == 0)
    {
        if (Player)
            br.setColor(Qt::white);
        else
            br.setColor(Qt::black);
        pt.setBrush(br);
        pt.drawEllipse(40 * ChessRow + 15, 40 * ChessCol + 15, 10, 10);
    }
}

void QtGobang::mouseMoveEvent(QMouseEvent* event)
{
    if (GameOver)
        return;
    int CBX = event->x(), CBY = event->y();
    ChessRow = -1;
    ChessCol = -1;
    if (CBX < 0 || CBX >600 || CBY < 0 || CBY > 600)
    {
        update();
        return;
    }
    ChessRow = CBX / 40;
    ChessCol = CBY / 40;
    update();
}